package com.zjut.an.util.tools;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import com.zjut.an.admin.dao.AnAdmin;
import com.zjut.an.admin.manager.AnAdminManager;
import com.zjut.an.adplace.dao.AnAdplace;
import com.zjut.an.adplace.manager.AnAdplaceManager;
import com.zjut.an.news.dao.AnNews;
import com.zjut.an.news.manager.AnNewsManager;
import com.zjut.an.role.dao.AnRole;
import com.zjut.an.role.manager.AnRoleManager;
import com.zjut.an.util.system.Status;
import com.zjut.an.webclass.dao.AnWebclass;
import com.zjut.an.webclass.dao.AnWebclassDAO;
import com.zjut.an.webclass.manager.AnWebclassManager;


public class HTMLCreat {
	
	@Resource(name = "AnWebclassManager")
	AnWebclassManager manager = new AnWebclassManager();
	
	@Resource(name = "AnNewsManager")
	AnNewsManager nmanager = new AnNewsManager();
	
	@Resource(name = "AnRoleManager")
	AnRoleManager rmanager = new AnRoleManager();
	
	@Resource(name = "AnAdplaceManager")
	AnAdplaceManager amanager = new AnAdplaceManager();
	
	@Resource(name = "AnAdminManager")
	AnAdminManager admanager = new AnAdminManager();
	
	@Resource(name = "AnRoleManager")
	AnRoleManager romanager = new AnRoleManager();
	
	@Resource(name = "AnWebclassDAO")
	AnWebclassDAO dao = new AnWebclassDAO();
	
	private Map<Integer, AnWebclass> map1;
	
	public HTMLCreat() {
		
	}
	
	private void creatClassMap() {//建立classmapbyParent以wcParent为key，其子栏目List为value的map, 建立classmap以wcId为key，webclass为value的map
		map1 = new HashMap<Integer, AnWebclass>();
		List webclasslist = manager.getDatas();
		for (Iterator it = webclasslist.iterator(); it.hasNext();) {
			AnWebclass webclass = (AnWebclass)it.next();
			map1.put(webclass.getWcId(), webclass);
		}
	}
	
	
	
	public StringBuffer getRoleSelectHTML(String value) {
		
		List rolelist = rmanager.getDatas();
		StringBuffer HTML = new StringBuffer();
		HTML.append("<select name='roId'>");
		for (Iterator it = rolelist.iterator(); it.hasNext();) {
			AnRole role = (AnRole)it.next();
			HTML.append("<option value='"+role.getRoId()+"'");
			if (value!=null && Integer.parseInt(value) == role.getRoId().intValue()) {
				HTML.append(" selected");
			}
			HTML.append(">"+role.getRoName()+"</option>");
		}
		HTML.append("</select>");
		return HTML;
	}
	
	public StringBuffer getAdplaceSelectHTML(String value) {
		
		List aplist = amanager.getDatas();
		StringBuffer HTML = new StringBuffer();
		HTML.append("<select name='apNo' multiple='' class='form-control'>");
		for (Iterator it = aplist.iterator(); it.hasNext();) {
			AnAdplace adplace = (AnAdplace)it.next();
			HTML.append("<option value='"+adplace.getApNo()+"'");
			if (value!=null && Integer.parseInt(value) == adplace.getApNo()) {
				HTML.append(" selected");
			}
			HTML.append(">"+adplace.getApName()+"</option>");
		}
		HTML.append("</select>");
		return HTML;
	}
	
	public StringBuffer getTopWebclassSelectHTML(String value) {
		
		List webclasslist = manager.getTopDatas();
		StringBuffer HTML = new StringBuffer();
		HTML.append("<option value='0' ");
		if(value==null || value.equals("0"))HTML.append("selected");
		HTML.append(">大栏目</option>");
		for (Iterator it = webclasslist.iterator(); it.hasNext();) {
			AnWebclass webclass = (AnWebclass)it.next();
			HTML.append("<option value='"+webclass.getWcId()+"'");
			if (value!=null && Integer.parseInt(value) == webclass.getWcId()) {
				HTML.append(" selected");
			}
			HTML.append(">"+webclass.getWcName()+"</option>");	
			
			List webclasslist2 = manager.getChildrenDatas(webclass.getWcId());
			for (Iterator it2 = webclasslist2.iterator(); it2.hasNext();) {
				AnWebclass webclass2 = (AnWebclass)it2.next();
				HTML.append("<option value='"+webclass2.getWcId()+"'");
				if (value!=null && Integer.parseInt(value) == webclass2.getWcId()) {
					HTML.append(" selected");
				}
				HTML.append(">"+webclass.getWcName()+"--"+webclass2.getWcName()+"</option>");	
			}
		}
		//HTML.append("</select>");
		return HTML;
	}
	
	/**
	 * 返回一个栏目选择下拉框
	 * @param selectClassid
	 * @return
	 */
	public StringBuffer getClassSelectHTML(Integer selectClassid, Integer adId) {
		this.creatClassMap();		
		AnAdmin admin = admanager.getData(adId);
		AnRole role = romanager.getData(admin.getRoId());		
		StringBuffer str = new StringBuffer();
		
		List list = manager.getDatas();
		for(int i=0; i<list.size(); i++) {
			AnWebclass obj = (AnWebclass)list.get(i);
			String selected = "";
			if (obj.getWcId().equals(selectClassid)) {
				selected = "selected";
			}
			str.append("<option value=\"" + obj.getWcId() + "\" "+selected+">" + getPreName(obj) + "</option>");
		}
		
		return str;
	}
	
	public StringBuffer getClassSelectsHTML(String wcIds, Integer adId) {//返回多选输入框
		//System.out.println("####1111111111111111111####");
		this.creatClassMap();
		
		AnAdmin admin = admanager.getData(adId);
		AnRole role = romanager.getData(admin.getRoId());
		StringBuffer str = new StringBuffer();
		
		List webclasslist = manager.getDatas();
		
		for (Iterator it = webclasslist.iterator(); it.hasNext();) {
			AnWebclass webclass = (AnWebclass)it.next();
			if (manager.getChildrenDatas(webclass.getWcId()).size()==0) {
				if (wcIds!=null && (","+wcIds+",").contains(String.valueOf(","+webclass.getWcId())+",")) {
					str.append("<option value=\"" + webclass.getWcId() + "\" selected>" + getPreName(webclass) + "</option>");
				} else {
					str.append("<option value=\"" + webclass.getWcId() + "\">" + getPreName(webclass) + "</option>");					
				}
			}
		}
		return str;
	}
	
	private String getPreName(AnWebclass webclass) {
		StringBuffer str = new StringBuffer();
		if (webclass.getWcParent()!=0) {
			AnWebclass webclass1 = map1.get(webclass.getWcParent());
			
			if (webclass1!=null && webclass1.getWcParent()!=0) {
				AnWebclass webclass2 = map1.get(webclass1.getWcParent());
				str.append(webclass2.getWcName()+"--");
			}
			str.append(webclass1.getWcName()+"--");
		}
		str.append(webclass.getWcName());
		return str.toString();
	}
	
	/**
	 * 返回一个栏目及其上级栏目的导航链接
	 * @param classid
	 * @return
	 */
	public StringBuffer getClassURL(AnWebclass webclass) {
		
		StringBuffer url = new StringBuffer();
		url.append("<li><a href='"+this.classlink(webclass)+"'>"+webclass.getWcName()+"</a>/</li>");
		
		AnWebclass class1 = dao.findById(webclass.getWcParent());
		if (class1!=null) {
			url.insert(0,"<li><a href='"+this.classlink(class1)+"'>"+class1.getWcName()+"</a>/</li>");
			
			if (class1.getWcParent()!=0) {
				AnWebclass class2 = dao.findById(class1.getWcParent());
				if (class2!=null) {
					url.insert(0,"<li><a href='"+this.classlink(class2)+"'>"+class2.getWcName()+"</a>/</li>");
				} 
			}
		} 
		
		url.insert(0,"<li><i class=\"icon-home\"></i><a href='../jsp/index.jsp'>首页</a>/</li>");
		return url;
	}
	
	public StringBuffer getClassURL(int classid) {
		AnWebclass webclass = dao.findById(classid);
		return getClassURL(webclass);
	}
	
	public String classlink(AnWebclass webclass) {
		if (webclass.getWcShowtype().equals(Status.CLASSSHOWTYPE_URL.getStatus())) {
			return webclass.getWcUrl();
		} else {
			return "newsclass.jsp?wcId="+webclass.getWcId();
			//return "w"+webclass.getWcId()+".html";
		} 
/*		if (webclass.getWcShowtype().equals(Status.CLASSSHOWTYPE_LIST.getStatus())) {
			return "newsclass.jsp?wcId="+webclass.getWcId();
			//return "w"+webclass.getWcId()+".html";
		} else if (webclass.getWcShowtype().equals(Status.CLASSSHOWTYPE_PAGE.getStatus())) {
			
			AnNews news = new AnNews();
			news.setWcId(webclass.getWcId());
			List newslist = nmanager.getDataByInstance(1, 1, null, null, news).getObjects();
			//System.out.println(webclass.getClassname()+"--"+webclass.getClassshowtype()+":"+newslist.size());
			if (newslist.size()>0) {
				return "newsclass.jsp?neId="+((AnNews)newslist.get(0)).getNeId();
			} 
			return "newsclass.jsp";
		} else if (webclass.getWcShowtype().equals(Status.CLASSSHOWTYPE_URL.getStatus())) {
			return webclass.getWcUrl();
		}*/
	}
	
	public String newslink(AnNews news) {
		if (news.getTitleurl()!=null && news.getTitleurl().trim().length()>0) {
			return news.getTitleurl();
		} else {
			return "zs_news.jsp?neId="+news.getNeId();
		}
	}
	
	public String newsCss(AnNews news) {
		if (Status.REDTITLE_YES.getStatus().equals(news.getRedtitle())) {
			return " style='color:red;'";
		} else {
			return "";
		}
	}
	
	
	public String newshtmllink(AnNews news) {
		if (news.getTitleurl()!=null && news.getTitleurl().trim().length()>0) {
			return news.getTitleurl();
		} else {
			return "../html/n"+news.getNeId()+".html";
		}
	}
}
